<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Merchandise extends MY_Admin {
    
    function __construct()
    {
        parent :: __construct();
        $this->lang->load('merchandise', 'chinese');
        $this->load->model('Merchandise_model');
    }

    public function index()
    {
        $config['base_url']         = site_url('admins/merchandise');
        $config['total_rows']       = 200;
        $config['per_page']         = $this->pageLimit;
        $config['uri_segment']      = 3;
        //$config['use_page_numbers'] = TRUE;
        //echo $this->uri->segment(3);
        $this->pagination->initialize($config);

        $parm['where'] = array();
        $datas['merchandises'] = $this->Merchandise_model->get_merchandises($parm, $this->uri->segment(3), $this->pageLimit);
        
        $this->load->view('admins/merchandise/list', $datas);
    }
    
    public function edit()
    {
        $data['merchandise'] = '';
        if($data['merchandise'] = $this->Merchandise_model->get_one($this->uri->segment(4)))
            $this->load->view('admins/merchandise/edit', $data);
        else
            $this->load->view('admins/merchandise/edit');
    }
    
    public function doEdit()
    {
        $validation = array(
            array(
                'field' => 'mer_name',
                'label' => lang('mer_name'),
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'mer_content',
                'label' => lang('mer_content'),
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'mer_moq',
                'label' => lang('mer_moq'),
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'mer_total',
                'label' => lang('mer_total'),
                'rules' => 'trim|numeric'
            )
        );

        // Set the validation rules
        $this->form_validation->set_rules($validation);
        $user_data = array();
        // Convert the array to an object
        $user_data['mer_name']		= $this->security->xss_clean($this->input->post('mer_name'));
        $user_data['mer_content'] 	= $this->security->xss_clean($this->input->post('mer_content'));
        
        $user_data['mer_type']      = $this->security->xss_clean($this->input->post('mer_type'));
        $user_data['tag']   	    = $this->security->xss_clean($this->input->post('tag'));
        $user_data['mer_price']   	= $this->security->xss_clean($this->input->post('mer_price'));
        $user_data['mer_moq']   	= $this->security->xss_clean($this->input->post('mer_moq'));
        $user_data['mer_total']   	= $this->security->xss_clean($this->input->post('mer_total'));
        $user_data['mer_edtime']   	= $this->security->xss_clean($this->input->post('mer_edtime'));
        if ($this->form_validation->run())
        {
            //$this->Entries->add($this->input->post());
            if($this->Merchandise_model->add($user_data))
                $this->goback(1);
            else
                $this->goback(0);
        }
        else
        {
            echo validation_errors(); 
        }
    }
}